String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/g,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/g,"");
}

document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
	var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
	return $A(children).inject([], function(elements, child) {
		var attributeValue = child.getAttribute(attribute);
		if(attributeValue != null) {
			if(!value || attributeValue == value) {
				elements.push(child);
			}
		}
		return elements;
	});
}

var ajaxObject = function (url, callbackFunction, tagId) {
  var that=this;
	this.updating = false;
	this.tagId = document.getElementById(tagId);
	this.tagId.style.position = "relative";
//	alert("caca nene");
//	this.waitmsg = new xwrtPopUp("Process Data");
//	this.tagId.appendChild(this.waitmsg.algo);
//	var oldtag = this.tagId.innerHTML;
  this.abort = function() {
		if (that.updating) {
			that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
		}  
	}  
	this.update = function(passData,postMethod) {
		if (that.updating) {
			return false; 
		}
    that.AJAX = null;
		if (window.XMLHttpRequest) {
			that.AJAX=new XMLHttpRequest();
		} else {
			that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (that.AJAX==null) {
			return false;
		} else {
			that.AJAX.onreadystatechange = function() {
				if (that.AJAX.readyState==4) {
					that.updating=false;
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
					that.AJAX=null;
				}
			}
			that.updating = new Date();
			if (/post/i.test(postMethod)) {
				var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
			} else {
				var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());
				that.AJAX.open("GET", uri, true);
				that.AJAX.send(null);
			}
			return true;
		}
	}  
	var urlCall = url;
	this.callback = callbackFunction || function () { };
}

var xwrtPopUp = function (strcontent) {
	this.height = "30px";
	this.width = "200px";
	this.top = 0;
	this.left = 0;
	var popup = document.createElement("<div>");
/*
	var text = document.createTextNode(strcontent); 
	popup.appendChild(text);
	popup.style.position="absolute";
	popup.style.backgroundColor="#FF8888";
	popup.style.left=this.left;
	popup.style.top=this.top;
	popup.style.height=this.height;
	popup.style.width=this.width;
	this.show = function(newcontent) {
		if (newcontent != '') strcontent = newcontent;
*/
//			popup.firstChild.innerHTML(strcontent);
/*
			popup.style.display = "block";
		}
	this.hidden = function () {
			popup.style.display = "none";
		}
	this.algo = popup;
*/
}

function pause(millisecondi)
{
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function init(){
	var myInstalled = new ajaxObject('installed_txt.sh', processInstalled,"installed_div");
//	var myPrueba = new ajaxObject('repo_txt.sh', processRepos,'prueba');
	myInstalled.update(); 
//	myPrueba.update(); 
}

addLoadEvent(init);

addLoadEvent(function() {
    document.body.style.backgroundColor = 'yellow';
})
addLoadEvent(function() {
    document.body.appendChild(document.createTextNode('Hungry'));;
})
addLoadEvent(function() {
    document.body.appendChild(document.createTextNode(' monkeys!'));;
})

function processRepos(responseText, responseStatus) {
  if (responseStatus==200) {
		var tableObj = document.createElement("TABLE");
		this.tagId.appendChild(tableObj);
		var color="#aabbcc";
		var temp = new Array();
		var aline = new Array();
		temp = responseText.split('\n');
		var crows = 1; //this.tagId.innerHTML.rows.length;
//		var txt = document.createTextNode("This text was added to the DIV.");
//		this.tagId.appendChild(txt); 
		tableObj.style.width="98%";
		tableObj.style.overflow="scroll";
		var newrow = tableObj.insertRow(0);
		newrow.className = "packages";
		var action=newrow.insertCell(0);
		var pkg=newrow.insertCell(1);
		var ver=newrow.insertCell(2);
		var desc=newrow.insertCell(3);
		action.innerHTL = "&nbsp";
		pkg.innerHTML = "@TR<<system_ipkg_th_package#Package>>";
		ver.innerHTML = "@TR<<system_ipkg_th_version#Version>>";
		desc.innerHTML = "@TR<<system_ipkg_th_desc#Description>>";
		for (x in temp)
		{
			if (temp[x].trim().length > 0){
				var values = temp[x].split(":");
				if (values[0] == "Package"){
					newrow = tableObj.insertRow(crows);
					newrow.className = "packages";
					color = (color == "#ccddee") ? "#aabbcc" : "#ccddee";
					newrow.style.backgroundColor=color;
					crows = crows + 1;
					var action=newrow.insertCell(0);
					var pkg=newrow.insertCell(1);
					var ver=newrow.insertCell(2);
					var desc=newrow.insertCell(3);
					action.innerHTML = "<a href=\"javascript:confirmT('remove','" + values[1] + "')\">@TR<<system_ipkg_Install#Install>></a>";
					pkg.innerHTML = values[1];
				}
				if (values[0] == "Version"){
					ver.innerHTML = values[1];
				}
				if (values[0] == "Description"){
					desc.innerHTML = values[1];
				}
			}
		}
	} else {
		this.tagId.innerHTML = responseStatus + ' -- Error Processing Request';
	}
//	this.waitmsg.hidden();
}

function processInstalled(responseText, responseStatus) {
  if (responseStatus==200) {
		var tableObj = document.createElement("TABLE");
		this.tagId.appendChild(tableObj);
		var color="#aabbcc";
		var temp = new Array();
		var aline = new Array();
		temp = responseText.split('\n');
		var crows = 1; //this.tagId.innerHTML.rows.length;
//		var txt = document.createTextNode("This text was added to the DIV.");
//		this.tagId.appendChild(txt); 
		tableObj.style.width="98%";
		tableObj.style.overflow="scroll";
		var newrow = tableObj.insertRow(0);
		newrow.className = "packages";
		var action=newrow.insertCell(0);
		var pkg=newrow.insertCell(1);
		var ver=newrow.insertCell(2);
		var desc=newrow.insertCell(3);
		action.innerHTL = "&nbsp";
		pkg.innerHTML = "@TR<<system_ipkg_th_package#Package>>";
		ver.innerHTML = "@TR<<system_ipkg_th_version#Version>>";
		desc.innerHTML = "@TR<<system_ipkg_th_desc#Description>>";
		for (x in temp)
		{
			if (temp[x].trim().length > 0){
				var values = temp[x].split(":");
				if (values[0] == "Package"){
					newrow = tableObj.insertRow(crows);
					newrow.className = "packages";
					color = (color == "#ccddee") ? "#aabbcc" : "#ccddee";
					newrow.style.backgroundColor=color;
					crows = crows + 1;
					var action=newrow.insertCell(0);
					var pkg=newrow.insertCell(1);
					var ver=newrow.insertCell(2);
					var desc=newrow.insertCell(3);
					action.innerHTML = "<a href=\"javascript:confirmT('remove','" + values[1] + "')\">@TR<<system_ipkg_Uninstall#Uninstall>></a>";
					pkg.innerHTML = values[1];
				}
				if (values[0] == "Version"){
					ver.innerHTML = values[1];
				}
			}
		}
	} else {
		this.tagId.innerHTML = responseStatus + ' -- Error Processing Request';
	}
//	this.waitmsg.hidden();
}

function processPrueba(responseText, responseStatus) {
  if (responseStatus==200) {
		var tableObj = document.createElement("TABLE");
		this.tagId.appendChild(tableObj);
		var color="#aabbcc";
		var temp = new Array();
		var aline = new Array();
		temp = responseText.split('\n');
		var crows = 1; //this.tagId.innerHTML.rows.length;
//		var txt = document.createTextNode("This text was added to the DIV.");
//		this.tagId.appendChild(txt); 
		tableObj.style.width="98%";
		tableObj.style.overflow="scroll";
		var newrow = tableObj.insertRow(0);
		newrow.className = "packages";
		var action=newrow.insertCell(0);
		var pkg=newrow.insertCell(1);
		var ver=newrow.insertCell(2);
		var desc=newrow.insertCell(3);
		action.innerHTL = "&nbsp";
		pkg.innerHTML = "@TR<<system_ipkg_th_package#Package>>";
		ver.innerHTML = "@TR<<system_ipkg_th_version#Version>>";
		desc.innerHTML = "@TR<<system_ipkg_th_desc#Description>>";
		for (x in temp)
		{
			if (temp[x].trim().length > 0){
				var values = temp[x].split(":");
				var key = values[0];
				var value = values[1];
				if (values[0] == "Package"){
					newrow = tableObj.insertRow(crows);
					newrow.className = "packages";
					color = (color == "#ccddee") ? "#aabbcc" : "#ccddee";
					newrow.style.backgroundColor=color;
					crows = crows + 1;
					var action=newrow.insertCell(0);
					var pkg=newrow.insertCell(1);
					var ver=newrow.insertCell(2);
					var desc=newrow.insertCell(3);
					action.innerHTML = "<a href=\"javascript:confirmT('remove','" + values[1] + "')\">@TR<<system_ipkg_Uninstall#Uninstall>></a>";
					pkg.innerHTML = values[1];
				}
				if (values[0] == "Version"){
					ver.innerHTML = values[1];
				}
			}
		}
	} else {
		this.tagId.innerHTML = responseStatus + ' -- Error Processing Request';
	}
//	this.waitmsg.hidden();
}
